package com.jilin.library.service;

import com.jilin.library.entity.BorrowRecord;
import com.jilin.library.mapper.BorrowRecordMapper;
import com.jilin.library.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class BorrowRecordService {
    @Autowired
    private BorrowRecordMapper borrowRecordMapper;
    
    @Autowired
    private BookMapper bookMapper;
    
    public List<BorrowRecord> findAll() {
        return borrowRecordMapper.findAll();
    }
    
    @Transactional
    public BorrowRecord borrowBook(BorrowRecord record) {
        // 减少图书库存
        bookMapper.updateStock(record.getBookId(), -1);
        borrowRecordMapper.insert(record);
        return record;
    }
    
    @Transactional
    public int returnBook(Integer id) {
        BorrowRecord record = borrowRecordMapper.findById(id);
        if (record != null && record.getStatus() == 0) {
            // 增加图书库存
            bookMapper.updateStock(record.getBookId(), 1);
            return borrowRecordMapper.returnBook(id);
        }
        return 0;
    }
} 